package com.yalemang.photovalut.ui.choice

import android.content.Intent
import androidx.recyclerview.widget.GridLayoutManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.yalemang.photovalut.base.BaseChickAdapter
import com.yalemang.photovalut.base.BaseSkinActivity
import com.yalemang.photovalut.base.ext.gone
import com.yalemang.photovalut.base.sql.entity.Folder
import com.yalemang.photovalut.base.user.Skin
import com.yalemang.photovalut.base.utils.ContentProviderUtils
import com.yalemang.photovalut.databinding.ActivityChoicePhotoBinding
import com.yalemang.photovalut.res.R.*
import com.yalemang.photovalut.ui.choice.adapter.ChoicePhotoAdapter
import com.yalemang.photovalut.ui.folder.PhotoFolderActivity
import com.yalemang.photovalut.ui.preview.PreviewPhotoActivity

class ChoicePhotoActivity: BaseSkinActivity<ActivityChoicePhotoBinding, ChoicePhotoPresenter>(),
    ChoicePhotoContract.ChoicePhotoView {

    companion object{
        const val KEY_PHOTO_PATH_LIST = "key_photo_path_list"
        const val KEY_PHOTO_PATH_SET = "key_photo_path_set"
    }

    private lateinit var photoList : ArrayList<String>
    private lateinit var choicePhotoAdapter: ChoicePhotoAdapter

    override fun switchSkin(skin: Skin) {
        mBinding.tbChoicePhoto.setBackgroundColor(skin.mainColor)
        mBinding.rlChoicePhotoAbove.setBackgroundColor(skin.mainColor)
        mBinding.rlChoicePhotoBottom.setBackgroundColor(skin.mainColor)
    }

    override fun initData() {

    }

    override fun initView() {
        mBinding.tbChoicePhoto.tvLeft.gone()
        mBinding.tbChoicePhoto.ivLeft.setImageResource(drawable.ic_back)
        mBinding.tbChoicePhoto.ivRight.gone()
        mBinding.tbChoicePhoto.tvRight.text = getString(string.choice_title_right)
        mBinding.tbChoicePhoto.ivCenter.gone()

        photoList = ContentProviderUtils.getImagePathList(this) as ArrayList<String>
        choicePhotoAdapter = ChoicePhotoAdapter(photoList)
        mBinding.rvChoicePhoto.layoutManager = GridLayoutManager(this, 4)
        mBinding.rvChoicePhoto.adapter = choicePhotoAdapter
        mBinding.tbChoicePhoto.tvRight.setOnClickListener{
            val photoPaths = choicePhotoAdapter.getPhotoPathSet()
            LiveEventBus.get(KEY_PHOTO_PATH_SET, HashSet::class.java)
                .post(photoPaths)
            finish()
        }

        choicePhotoAdapter.itemClickListener = { _: String, _: Int ->
            val intent = Intent(this@ChoicePhotoActivity, PreviewPhotoActivity::class.java)
            intent.putExtra(KEY_PHOTO_PATH_LIST, photoList)
            startActivity(intent)
        }
    }

    override fun initMVP() {
        mPresenter = ChoicePhotoPresenter()
        mPresenter.mView = this
        mPresenter.mModel = ChoicePhotoModel()
    }
}